package com.example.controller;

import cn.hutool.core.util.IdUtil;
import com.example.common.Result;
import com.example.common.ResultUtils;
import com.example.dto.CreateOrderRequest;
import com.example.dto.OrderMsg;
import com.example.mq.sender.IMsgSender;
import com.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private IMsgSender msgSender;

    /*
     * 创建订单，模拟投递消息
     * @param req
     * @return
     */
    @PostMapping("/createOrder")
    public Result<String> createOrder(@Validated @RequestBody CreateOrderRequest req)
    {
        return ResultUtils.success(this.orderService.createOrder(req));
    }



    /*
     * @param
     * @==============History===============<br/>;
     * @Description //   发送顺序消息
     * @Date 2025/8/25
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @PostMapping("/sendSequential")
    public Result<Void> sendSequential()
    {
        String orderId = IdUtil.fastSimpleUUID();

        List<String> list = Arrays.asList("订单创建消息",
                "订单支付消息",
                "订单已发货",
                "买家确认收货",
                "订单已完成");
        for (String type : list)
        {
            msgSender.sendSequentialWithBody(orderId,
                    "orderExchange",
                    "orderQueue1",
                    OrderMsg.builder().orderId(orderId).type(type).build());
        }
        return ResultUtils.success();
    }


}
